package com.xuexiang.xuidemo.page.uilts;

import ohos.aafwk.content.Intent;
import ohos.accessibility.ability.SoftKeyBoardController;
import ohos.accessibility.ability.SoftKeyBoardListener;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.service.WindowManager;

import com.xuexiang.xui_lib.component.button.switchbutton.SwitchButton;
import com.xuexiang.xui_lib.component.toast.XToast;
import com.xuexiang.xui_lib.util.KeyboardUtils;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.page.base.BasePageAbilitySlice;
import com.xuexiang.xuidemo.page.base.Page;

/**
 * KeyBoardUtils示例页面
 *
 * @since 2021-03-23
 */
@Page(name = "KeyBoardUtils", action = "KeyBoardUtils")
public class KeyBoardUtilsAbilitySlice extends BasePageAbilitySlice
    implements SoftKeyBoardListener, Component.ClickedListener, KeyboardUtils.SoftKeyboardToggleListener {
    private SwitchButton aSwitch;
    private Button getStatus, switchScreen, showSoft, hideSoft;
    private TextField textField1, textField2;
    private boolean isShowStatus = true;

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_utils_key_board;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        init();
        getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_RESIZE);
    }

    public void init() {
        getStatus = findCastComponentById(ResourceTable.Id_key_board_get_status);
        switchScreen = findCastComponentById(ResourceTable.Id_key_board_switch_screen);
        showSoft = findCastComponentById(ResourceTable.Id_key_board_show_soft);
        hideSoft = findCastComponentById(ResourceTable.Id_key_board_hide_soft);
        getStatus.setClickedListener(this);
        switchScreen.setClickedListener(this);
        showSoft.setClickedListener(this);
        hideSoft.setClickedListener(this);

        textField1 = findCastComponentById(ResourceTable.Id_key_board_textfield1);
        textField2 = findCastComponentById(ResourceTable.Id_key_board_textfield2);
        aSwitch = findCastComponentById(ResourceTable.Id_key_board_switch);
        aSwitch.setTouchEventListener((component, touchEvent) -> false);

        SoftKeyBoardController softKeyBoardController = new SoftKeyBoardController(textField1.getId(), this);
        softKeyBoardController.addListener(this);

        KeyboardUtils.addKeyboardToggleListener(aSwitch, this);
    }

    @Override
    public void onSoftKeyBoardShowModeChanged(SoftKeyBoardController softKeyBoardController, int i) {
        XToast.normal(getContext(), softKeyBoardController.getShowMode() + "").show();
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_key_board_show_soft:
                KeyboardUtils.showSoftInput();
                break;
            case ResourceTable.Id_key_board_hide_soft:
                textField1.clearFocus();
                textField2.clearFocus();
                KeyboardUtils.hideSoftInput();
                break;
            case ResourceTable.Id_key_board_switch_screen:
                if (isShowStatus) {
                    // 隐藏状态栏
                    getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
                    isShowStatus = false;
                } else {
                    isShowStatus = true;
                    getWindow().clearFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
                }
                break;
            case ResourceTable.Id_key_board_get_status:
                XToast.normal(getContext(), "键盘显示状态:" + KeyboardUtils.isSoftInputShow(component)).show();
                break;
        }
    }

    @Override
    public void onToggleSoftKeyboard(boolean isVisible) {
        if (aSwitch != null) {
            aSwitch.setChecked(isVisible);
        }
    }
}
